<?php
// 
//  MY_strng_helper.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-06-17.
//  Copyright 2011 lishuzu. All rights reserved.
// 
// 得到首字母
function getfirstletter($string) {
	$string=biconv($string,'gbk','utf-8');
	$dict=array(
		'a'=>0xB0C4,'b'=>0xB2C0,'c'=>0xB4ED,'d'=>0xB6E9,
		'e'=>0xB7A1,'f'=>0xB8C0,'g'=>0xB9FD,'h'=>0xBBF6,
		'j'=>0xBFA5,'k'=>0xC0AB,'l'=>0xC2E7,'m'=>0xC4C2,
		'n'=>0xC5B5,'o'=>0xC5BD,'p'=>0xC6D9,'q'=>0xC8BA,
		'r'=>0xC8F5,'s'=>0xCBF9,'t'=>0xCDD9,'w'=>0xCEF3,
		'x'=>0xD1B8,'y'=>0xD4D0,'z'=>0xD7F9,
		);
	$letter = substr($string, 0, 1);
	$letter_ord = ord($letter);
	if($letter_ord >= 176 && $letter_ord <= 215){
		$num = '0x'.bin2hex(substr($string, 0, 2));
		foreach ($dict as $k=>$v){
			if($v>=$num) break;
		}
		return $k;
	}elseif(($letter_ord>64 && $letter_ord<91) || ($letter_ord>96 && $letter_ord<123)){
		return $letter;
	}elseif($letter>='0' && $letter<='9'){
		return $letter;
	}else{
		return '*';
	}
}

function biconv($str,$to='',$from='',$force=false) {
	if (empty($str)) return $str;
	if(!preg_match( '/[\x80-\xff]/', $str)) return $str; // is contain chinese char
	if(empty($to)){
		$to='utf-8';
	}
	if(empty($from)){
		$from = ('gbk'==strtolower($to)) ? 'utf-8':'gbk';
	}
	$to=strtolower($to);
	$from=strtolower($from);
	//$isutf8=preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $str );
	$re = strlen($str) > 6 ? '/([\xe0-\xef][\x80-\xbf]{2}){2}/' : '/[\xe0-\xef][\x80-\xbf]{2}/';
	$isutf8 = preg_match($re, $str);
	
	//$force = (substr($to, 0, 3) == 'utf') ? true : $force;
	
	if(!$force && $isutf8 && $to=='utf-8' ) return $str;
	if(!$force && !$isutf8 && $to=='gbk' ) return $str;
	
	if (function_exists('iconv')){
		$str = iconv($from, $to, $str);
	}else{
		require_once(APPPATH.'/libraties/Chinese.php');
		$ch = new chinese($from,$to);
		if('utf-8'==$from){
			$str = addslashes($ch->convert(stripslashes($str)));
		}else{
			$str = $ch->convert($str);
		}
	}
	return $str;
}


//获取字符串
function getstr($string, $length, $in_slashes=0, $out_slashes=0, $censor=0, $bbcode=0, $html=0) {
	$CI =& get_instance();

	$string = trim($string);

	if($in_slashes) {
		//传入的字符有slashes
		$string = sstripslashes($string);
	}
	if($html < 0) {
		//去掉html标签
		$string = preg_replace("/(\<[^\<]*\>|\r|\n|\s|\[.+?\])/is", ' ', $string);
		$string = shtmlspecialchars($string);
	} elseif ($html == 0) {
		//转换html标签
		$string = shtmlspecialchars($string);
	}
	if($censor) {
		//词语屏蔽
		//加载配置缓存文件
		@include_once(APPPATH.'cache/data_censor.php');
		
		if(@$config['censor']['banned'] && preg_match(@$config['censor']['banned'], $string)) {
			showmessage('information_contains_the_shielding_text');
		} else {
			$string = empty($config['censor']['filter']) ? $string :
				@preg_replace($config['censor']['filter']['find'], $config['censor']['filter']['replace'], $string);
		}
	}
	if($length && strlen($string) > $length) {
		//截断字符
		$wordscut = '';
		if(strtolower($CI->config->item['charset']) == 'utf-8') {
			//utf8编码
			$n = 0;
			$tn = 0;
			$noc = 0;
			while ($n < strlen($string)) {
				$t = ord($string[$n]);
				if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
					$tn = 1;
					$n++;
					$noc++;
				} elseif(194 <= $t && $t <= 223) {
					$tn = 2;
					$n += 2;
					$noc += 2;
				} elseif(224 <= $t && $t < 239) {
					$tn = 3;
					$n += 3;
					$noc += 2;
				} elseif(240 <= $t && $t <= 247) {
					$tn = 4;
					$n += 4;
					$noc += 2;
				} elseif(248 <= $t && $t <= 251) {
					$tn = 5;
					$n += 5;
					$noc += 2;
				} elseif($t == 252 || $t == 253) {
					$tn = 6;
					$n += 6;
					$noc += 2;
				} else {
					$n++;
				}
				if ($noc >= $length) {
					break;
				}
			}
			if ($noc > $length) {
				$n -= $tn;
			}
			$wordscut = substr($string, 0, $n);
		} else {
			for($i = 0; $i < $length - 1; $i++) {
				if(ord($string[$i]) > 127) {
					$wordscut .= $string[$i].$string[$i + 1];
					$i++;
				} else {
					$wordscut .= $string[$i];
				}
			}
		}
		$string = $wordscut;
	}
	if($bbcode) {
		$CI->load->helper('bbcode');
		$string = bbcode($string, $bbcode);
	}
	if($out_slashes) {
		$string = saddslashes($string);
	}
	return trim($string);
}

//去掉slassh
function sstripslashes($string) {
	if(is_array($string)) {
		foreach($string as $key => $val) {
			$string[$key] = sstripslashes($val);
		}
	} else {
		$string = stripslashes($string);
	}
	return $string;
}

function cutstr($string, $length, $dot = ' ...', $charset = 'utf8') {

	if(strlen($string) <= $length) {

		return $string;

	}
	$string = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array('&', '"', '<', '>'), $string);
	$strcut = '';
	if(strtolower($charset) == 'utf-8') {
		$n = $tn = $noc = 0;
		while($n < strlen($string)) {
			$t = ord($string[$n]);

			if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {

				$tn = 1; $n++; $noc++;

			} elseif(194 <= $t && $t <= 223) {

				$tn = 2; $n += 2; $noc += 2;

			} elseif(224 <= $t && $t < 239) {

				$tn = 3; $n += 3; $noc += 2;

			} elseif(240 <= $t && $t <= 247) {

				$tn = 4; $n += 4; $noc += 2;

			} elseif(248 <= $t && $t <= 251) {

				$tn = 5; $n += 5; $noc += 2;

			} elseif($t == 252 || $t == 253) {

				$tn = 6; $n += 6; $noc += 2;

			} else {

				$n++;
			}
			if($noc >= $length) {
				break;
			}
		}
		if($noc > $length) {
			$n -= $tn;
		}
		$strcut = substr($string, 0, $n);

	} else {
		for($i = 0; $i < $length; $i++) {
			$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
		}
	}
	$strcut = str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $strcut);
	return $strcut.$dot;
}
//SQL ADDSLASHES

function saddslashes($string) {
	if(is_array($string)) {
		foreach($string as $key => $val) {
			$string[$key] = saddslashes($val);
		}
	} else {
		$string = addslashes($string);
	}
	return $string;
}
?>